/**
 * 
 */
package br.com.ufrn.aipetro.ga.model.persist.hibernate;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;

import br.com.ufrn.aipetro.ga.domain.entity.device.controller.Controller;
import br.com.ufrn.aipetro.ga.persist.interfaces.BasicDao;


/**
 * 
 * @author Andre
 *
 * @param <T>
 */
public class HibernateBasicDao<T> implements BasicDao<T> {
	
	private Session session;
	private Class entityClass;
	
	/**
	 * 
	 */
	public HibernateBasicDao(Session session, Class entityClass) {
		this.session = session;
		this.entityClass = entityClass;
	}

	@Override
	public void delete(T entity) {
		session.delete(entity);
	}

	@Override
	public void insert(T entity) {
		session.save(entity);
	}

	@Override
	public T search(Serializable id) {
		return (T)session.get(this.entityClass, id);
	}
	
	@Override
	public List<T> searchAll() {
		return (List<T>)session.createCriteria(this.entityClass).list();
	}

	@Override
	public void update(T entity) {
		session.update(entity);
	}

	@Override
	public List<T> searchByExample(T example) {
		Example ex = Example.create(example)
				.enableLike(MatchMode.START)
				.excludeZeroes();
		return (List<T>)session.createCriteria(entityClass)
				.add(ex)
				.list();
	}

}
